package com.xkdx.guangguang.fragment.shop.brandofshop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.fragment.brand.branch.BranchFragment;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.shareclass.Shop;

public class BrandsOfShopFragment extends BaseFragment implements
		OnClickListener {
	View view;
	TextView entertaiment_tv, shopping_tv, life_tv, food_tv, delete_tv;
	EditText brand_search_et;
	Button back, floor;
	ImageLoader imageLoader = ImageLoader.getInstance();
	List<Shop> temp_list;
	List<Shop> entertaiment_list = new ArrayList<Shop>(),
			shopping_list = new ArrayList<Shop>(),
			life_list = new ArrayList<Shop>(),
			food_list = new ArrayList<Shop>();
	ListView listView;
	String shopID;
	DisplayImageOptions options;
	BrandsOfShopAction brandsOfShopAction;
	BrandsOfShopModule brandsOfShopModule;
	BrandsOfShopPresistence brandsOfShopPresistence;

	// 搜素商场品牌
	BrandSearchAction brandSearchAction;
	BrandSearchModule brandSearchModule;
	BrandSearchPresistence brandSearchPresistence;
	boolean isSearchCancle = false;
	MyAdapter adapter;
	ProgressBar pb;
	ImageView search;
	TextView delete_search_keyword;
	String type = "1";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.empty_photo)
				.showImageForEmptyUri(R.drawable.empty_photo)
				.showImageOnFail(R.drawable.empty_photo).cacheInMemory()
				.cacheOnDisc().displayer(new RoundedBitmapDisplayer(0)).build();
		shopID = getArguments().getString("ShopID");
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.brands_of_shop, container, false);
		findView();
		setOnClick();
		showLoading();

		return view;
	}

	@Override
	public void findView() {
		search = (ImageView) view.findViewById(R.id.im_search);
		delete_search_keyword = (TextView) view.findViewById(R.id.delete_et);
		brand_search_et = (EditText) view.findViewById(R.id.search_et);
		listView = (ListView) view.findViewById(R.id.shop_brand2_listview);
		entertaiment_tv = (TextView) view.findViewById(R.id.tab2);
		shopping_tv = (TextView) view.findViewById(R.id.tab1);
		life_tv = (TextView) view.findViewById(R.id.tab3);
		food_tv = (TextView) view.findViewById(R.id.tab4);
		back = (Button) view.findViewById(R.id.brands_of_shop_back);
		floor = (Button) view.findViewById(R.id.brands_of_shop_floors);
	}

	@Override
	public void setOnClick() {
		search.setOnClickListener(this);
		entertaiment_tv.setOnClickListener(this);
		shopping_tv.setOnClickListener(this);
		life_tv.setOnClickListener(this);
		food_tv.setOnClickListener(this);
		back.setOnClickListener(this);
		floor.setOnClickListener(this);
		delete_search_keyword.setOnClickListener(this);
		brandsOfShopAction = new BrandsOfShopAction(getArguments().getString(
				"ShopID"));
		brandsOfShopModule = new BrandsOfShopModule();
	}

	@Override
	public void showLoading() {
		dataLoadDialog.show();
		brandsOfShopPresistence = new BrandsOfShopPresistence(this);
		brandsOfShopPresistence.setActitons(brandsOfShopAction);
		brandsOfShopPresistence.setModule(brandsOfShopModule);
		brandsOfShopPresistence.execute();
		dataLoadDialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				if (brandsOfShopPresistence != null) {
					brandsOfShopPresistence.cancel(true);
				}
				getFragmentManager().popBackStack();

			}
		});
	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {
		dataLoadDialog.dismiss();
		if (map.containsKey("0")) {
			temp_list = brandsOfShopModule.list;
			if (temp_list != null && temp_list.size() != 0) {
				setValuses();
			} else {
				Toast.makeText(getActivity(), "品牌为空", Toast.LENGTH_SHORT)
						.show();
			}

		} else {
			refresh(map);
		}
	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {
		dataLoadDialog.dismiss();
		if (isSearchCancle) {
			return;
		}
		if (map.containsKey("0")) {
			temp_list = brandSearchModule.list;
			if (temp_list != null && temp_list.size() != 0) {
				setSearchValuses();
			} else {
				brand_search_et.setText("");
				Toast.makeText(getActivity(), "搜索品牌为空", Toast.LENGTH_SHORT)
						.show();
			}
		}
	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {
		// TODO Auto-generated method stub

	}

	class MyAdapter extends BaseAdapter {

		// private ImageLoader imageLoader;
		String url;
		Context context;

		public MyAdapter(Context context) {
			super();
			this.context = context;
			// imageLoader = ImageLoader.getInstance(context);

		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return temp_list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return temp_list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if (temp_list == null || temp_list.size() == 0) {
				return null;
			}
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(
						R.layout.shop_normal_item, null);
				holder = new ViewHolder();
				holder.im = (ImageView) convertView
						.findViewById(R.id.series_item_image);
				holder.card_zhe = (ImageView) convertView
						.findViewById(R.id.card_zhe);
				holder.name = (TextView) convertView.findViewById(R.id.name);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.card_zhe.setVisibility(View.GONE);
			url = temp_list.get(position).getLogo();
			if (url != null) {
				imageLoader.displayImage(url, holder.im, options);
			}
			if (temp_list.get(position).getCard().equals("True")) {
				holder.card_zhe.setVisibility(View.VISIBLE);
			}

			holder.name.setText(temp_list.get(position).getShopName());

			return convertView;
		}

		class ViewHolder {
			TextView name;
			ImageView im, card_zhe;
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tab1:
			chang(1);
			break;
		case R.id.tab2:
			chang(2);
			break;
		case R.id.tab3:
			chang(3);
			break;
		case R.id.tab4:
			chang(4);
			break;
		case R.id.brands_of_shop_back:
			getFragmentManager().popBackStack();
			break;
		case R.id.brands_of_shop_floors:
			FloorsOfShopFragment floorsOfShopFragment = new FloorsOfShopFragment();
			Bundle b = new Bundle();
			b.putString("shopID", shopID);
			floorsOfShopFragment.setArguments(b);
			getFragmentManager().beginTransaction()
					.replace(R.id.fragment, floorsOfShopFragment)
					.addToBackStack(null).commit();
			break;
		case R.id.im_search:
			String keyword = brand_search_et.getText().toString();
			if (keyword.equals("")) {
				Toast.makeText(getActivity(), "请输入搜索品牌...", Toast.LENGTH_SHORT)
						.show();
			} else {
				IConstants.hideInputMethodAways(getActivity(), brand_search_et);
				dataLoadDialog.setMessage("正在搜素...");
				dataLoadDialog.show();
				brandSearchAction = new BrandSearchAction(shopID,
						brand_search_et.getText().toString(), type);
				brandSearchModule = new BrandSearchModule();
				brandSearchPresistence = new BrandSearchPresistence(this);
				brandSearchPresistence.setActitons(brandSearchAction);
				brandSearchPresistence.setModule(brandSearchModule);
				brandSearchPresistence.execute();
				dataLoadDialog.setOnCancelListener(new OnCancelListener() {

					@Override
					public void onCancel(DialogInterface dialog) {
						if (brandSearchPresistence != null) {
							brandSearchPresistence.cancel(true);
							isSearchCancle = true;
						}

					}
				});
			}
			break;
		case R.id.delete_et:
			brand_search_et.setText("");
			break;
		default:
			break;
		}

	}

	private void setValuses() {
		shopping_list.clear();
		entertaiment_list.clear();
		life_list.clear();
		food_list.clear();
		for (Shop shop : temp_list) {
			if (shop.getShopType().equals("1")) {

				shopping_list.add(shop);
			} else if (shop.getShopType().equals("2")) {

				entertaiment_list.add(shop);
			} else if (shop.getShopType().equals("3")) {

				life_list.add(shop);
			} else {

				food_list.add(shop);
			}
		}
		adapter = new MyAdapter(getActivity());
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				BranchFragment branchFragment = new BranchFragment();
				Bundle b = new Bundle();
				b.putString("shopID", temp_list.get(position).getShopID());
				branchFragment.setArguments(b);
				getFragmentManager().beginTransaction()
						.replace(R.id.fragment, branchFragment)
						.addToBackStack(null).commit();

			}
		});
	}

	private void setSearchValuses() {

		if (type.equals("1")) {
			temp_list = shopping_list = brandSearchModule.list;
		} else if (type.equals("2")) {
			temp_list = entertaiment_list = brandSearchModule.list;
		} else if (type.equals("3")) {
			temp_list = life_list = brandSearchModule.list;
		} else {
			temp_list = food_list = brandSearchModule.list;
		}

		adapter = new MyAdapter(getActivity());
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				BranchFragment branchFragment = new BranchFragment();
				Bundle b = new Bundle();
				b.putString("shopID", temp_list.get(position).getShopID());
				branchFragment.setArguments(b);
				getFragmentManager().beginTransaction()
						.replace(R.id.fragment, branchFragment)
						.addToBackStack(null).commit();

			}
		});
	}

	protected void chang(int i) {
		switch (i) {
		case 1:
			if (shopping_list.size() != 0) {
				type = "1";
				brand_search_et.setText(null);
				shopping_tv
						.setBackgroundResource(R.drawable.shopinfo_subview_title_click);
				life_tv.setBackgroundResource(0);
				food_tv.setBackgroundResource(0);
				entertaiment_tv.setBackgroundResource(0);
				temp_list = shopping_list;
				adapter.notifyDataSetChanged();
			} else {
				Toast.makeText(getActivity(), "品牌为空", Toast.LENGTH_SHORT)
						.show();
			}
			break;
		case 2:
			if (entertaiment_list.size() != 0) {
				type = "2";
				brand_search_et.setText(null);
				entertaiment_tv
						.setBackgroundResource(R.drawable.shopinfo_subview_title_click);
				life_tv.setBackgroundResource(0);
				food_tv.setBackgroundResource(0);
				shopping_tv.setBackgroundResource(0);
				temp_list = entertaiment_list;
				adapter.notifyDataSetChanged();
			} else {
				Toast.makeText(getActivity(), "品牌为空", Toast.LENGTH_SHORT)
						.show();
			}
			break;
		case 3:
			if (life_list.size() != 0) {
				type = "3";
				brand_search_et.setText(null);
				life_tv.setBackgroundResource(R.drawable.shopinfo_subview_title_click);
				entertaiment_tv.setBackgroundResource(0);
				food_tv.setBackgroundResource(0);
				shopping_tv.setBackgroundResource(0);
				temp_list = life_list;
				adapter.notifyDataSetChanged();
			} else {
				Toast.makeText(getActivity(), "品牌为空", Toast.LENGTH_SHORT)
						.show();
			}
			break;
		case 4:
			if (food_list.size() != 0) {
				type = "4";
				brand_search_et.setText(null);
				food_tv.setBackgroundResource(R.drawable.shopinfo_subview_title_click);
				life_tv.setBackgroundResource(0);
				entertaiment_tv.setBackgroundResource(0);
				shopping_tv.setBackgroundResource(0);
				temp_list = food_list;
				adapter.notifyDataSetChanged();
			} else {
				Toast.makeText(getActivity(), "品牌为空", Toast.LENGTH_SHORT)
						.show();
			}
			break;
		default:
			break;
		}
	}
}
